<?php
header('Content-Type: text/html');
require_once dirname(__FILE__) . '/../../../incls/php/dbutils.class.php';
require_once dirname(__FILE__) . '/../../../incls/php/miscutils.class.php';
require_once dirname(__FILE__) . '/../../../incls/php/config.php';
require_once dirname(__FILE__) . '/../../../libs/phpMailer/class.phpmailer.php';
session_name(SESSION);
session_start();
$myPdo = DbUtils::createPdoInst();
$action = $_POST['action'];
try {
  $myPdo->beginTransaction();
  switch ($action) {
    case 'update':
      update($myPdo);
      break;
    default:
      break;
  }
  $myPdo->commit();
} catch (PDOException $e) {
  echo $e->getMessage();
}

function update($myPdo) {
  unset($_SESSION['pass']);
  unset($_SESSION['pass_re']);
  $url = MiscUtils::getParam('url', '');
  $from = MiscUtils::getParam('from', '');
  $pass = MiscUtils::getParam('pass', '');
  $pass_re = MiscUtils::getParam('pass_re', '');
  $id = MiscUtils::getParam('id', '');
  $is_err = 0;
  if (strlen($pass) < 6 || strlen($pass) > 16) {
    $_SESSION['pass'] = 'Password length must be 6 - 16 chars';
    $is_err = 1;
  }
  if ($pass != $pass_re) {
    $_SESSION['pass_re'] = 'Confirm password is incorrect';
    $is_err = 1;
  }
  if ($is_err == 1) {
    echo "
    <script>
    parent.location.href = '$from';
    </script>
    ";
    return;
  }
  $obj = new stdClass();
  $obj->id = $id;
  $obj->password = md5($pass);
  DbUtils::update($myPdo, TABLE_NAME_PREFIX_PLUGIN . 'ecom_customer', $obj);
  echo "
  <script>
  parent.location.href = '$url';
  </script>
  ";
}
?>